package com.trf.library.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trf.library.entity.Admin;
import com.trf.library.mapper.AdminMapper;
import com.trf.library.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 小小唐
 * @Date 2023/4/8-星期六-0:39
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 查询admin列表，如果有名，根据名模糊查询，否则返回全部admin用户
     *
     * @param name
     * @return List<Admin>
     */
    @Override
    public List<Admin> getAdminList(String name) {
        List<Admin> adminList = null;
        if (name == null || name == "") {
            adminList = adminMapper.selectList(null);
        }else {
            QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
            adminMapper.selectList(queryWrapper.like("%"+"name"+"%",name));
        }
        return adminList;
    }
    @Override
    public int deleteAdminById(String id) {
        return adminMapper.deleteById(id);
    }

    @Override
    public boolean login(String id, String password) {
        Admin admin = findAdmin(id);
        if (admin == null || !admin.getPassword().equals(password)) {
            return false;
        }
        return true;
    }

    @Override
    public Admin findAdmin(String id) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        return adminMapper.selectOne(wrapper.eq("id", id));
    }

    @Override
    public int updateAdmin(Admin admin) {
        return adminMapper.updateById(admin);
    }

    @Override
    public int addAdmin(Admin admin) {
        return adminMapper.insert(admin);
    }

    @Override
    public boolean haveAdmin(String id) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        return adminMapper.selectOne(wrapper.eq("id", id)) != null;
    }
}
